<%@ page import="dk.itu.iwjx.beans.*" %>
<%@ page import="dk.itu.iwjx.helper.*" %>
<%@ page import="java.util.*" %>
<%@ page errorPage="global/error.jsp" %>
<%
	String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
%>

<%-- Yijie: suggest to move the following header to the global header.jsp  --%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%-- looks like we must spicify the package name itu.dk.iwjx.beans althrough we have made it imported --%>
<jsp:useBean id="pizza" scope="request" class="dk.itu.iwjx.beans.PizzaBean" />
<jsp:useBean id="cart" scope="session" class="dk.itu.iwjx.beans.CartBean" />


<head>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> -->
<title>--header--<%=path %></title>
    <link type="text/css" href="asset/css/style.css" rel="Stylesheet" />
</head>

<body>
<div id="wrapper">

	<%-- start of top --%>
	<div id="top">
		<div id="logo">
		</div>
		
		<div id="navi">
			<a href="index.jsp">Home</a>
			<a href="#">Pizza</a>
			<a href="#">Chat</a>
			<a href="#">About Us</a>
		</div>
	</div>	
	<%-- end of top --%>
	
	<%-- start of main body --%>
	<div id="main">
	
		<%-- start of left part of body --%>
		<div id="left">
		<%if(session.getAttribute("user") == null){%>
		
		<form action="login.do" method="post">
			<table width="25%">
				<tr>
					<td colspan="2"><h3>User Login</h3></td>
				</tr>
				<tr>
					<td>ID</td>
					<td><input type="text" name="id"></td>
				</tr>
				<tr>
					<td>Password</td>
					<td><input type="password" name="password"></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="submit" value="submit">
				 		<a href="register.jsp">Register</a>
					</td>
				</tr>
			</table>
		</form>
		<% }else{ %>
			<h2> Welcome <%= ((UserBean)session.getAttribute("user")).getName() %></h2>
			<a href="logOut.do">log Out</a>
		<% } %>
		</div>
		<%-- end  of left part of body--%>
		
		
		<%-- end  of middle part of body--%>
		<div id="middle">
		 <h3>Menu List:</h3> 
		 <div id="sortby"> sort by: <a href="sort.do?sortby=price">price</a> | 
		 							<a href="sort.do?sortby=name">name </a>
		 </div>
		 <table>
		 	<tr>
		  		<th>Name</th>
		  		<th>Description</th>
		  		<th>Image</th>
		  		<th>Price</th>
				<th></th>
		  	</tr>	 
		 	<% 	Map<Integer,PizzaBean> pizzaMap=null;
		 	
		 		//check if there is a sorted pizza list
	 			List<PizzaBean> pizzaList=
					(List<PizzaBean>)session.getAttribute("sortedPizzaList");
		 		
		 		if(pizzaList==null){
		 			//check if there is an unsorted pizza list
		 			pizzaMap=(Map<Integer,PizzaBean>)session.getAttribute("pizzaList");
		 			if(pizzaMap==null){
		 				PizzaHelper ph=Factory.instance().getPizzaHelper();
		 				pizzaMap=ph.getAllPizzas();
		 				session.setAttribute("pizzaList",pizzaList);
		 			}
		 			pizzaList=(List<PizzaBean>)pizzaMap.values();
		 		}
		 		
		 		//display all pizzas
		 		for(PizzaBean pb: pizzaList){
		 	%>
		  	<tr>
		  		<td><%= pb.getName() %></td>
		  		<td><%= pb.getDescription() %></td>
		  		<td><%= pb.getImg() %></td>
		  		<td><%= pb.getPrice() %></td>
		  		<td><a href="add.do?id=<%= pb.getId() %>"> + </a></td>
		  	</tr>
		  <% } %>
		  
		  </table>
		</div>
		<%-- end  of middle part of body--%>		
				
				
		<%-- start  of right part of body--%>		
		<div id="right">
			<h3>Your Cart:</h3>
			<table>
				<tr>
					<th>Name</th>
					<th>Price</th>
					<th>Qut</th>
					<th></th>
			 	</tr>
			<% 	//creat a baseket
				Map<Integer,ItemBean> items=(Map<Integer,ItemBean>)cart.getItems();
				if(!items.isEmpty()){
					for(ItemBean ib:items.values()){
						pizza=pizzaList.get(ib.getPizzaId());
			%>			
					<tr>
						<td><%= pizza.getName() %></td>
						<td><%= ib.getQuantity() %></td>
						<td><%= pizza.getPrice() %></td>
						<td><a href="remove.do?id=<%= ib.getPizzaId() %>"> - </a></td>
					</tr>
			<%							
					}
				}
			%>
				<tr>
					<td colspan="4" align="right">total price: <%= cart.getTotalPrice() %> </td>
				</tr>
				<tr>
					<td colspan="4"><a href="">proceed to check out</a></td>
				</tr>
			</table>
		</div>	
		<%-- end  of right part of body--%>
		
		
	</div>
	<%-- end of main body --%>
	
	<div class="clear"></div>
<%@ include file="global/footer.jsp" %>

